/*
 * code.c
 *
 *  Created on: May 7, 2022
 *      Author: Abyss
 */
#include "config.h"

uint8_t buffer,buffers[1024],buffer_flag,electric[24],electric_buffer,electric_cnt;

uint16_t buffer_cnt = 0;
uint32_t sys_time;

//紫罗兰  开机
uint8_t VIOLET[] = {138,43,226};
//白色
uint8_t WHITE[] = {255,255,255};
//黑色
uint8_t BLACK[] = {0,0,0};
//红色   继电器闭合
uint8_t RED[] = {255,0,0};
//绿色    继电器断开
uint8_t GREEN[] = {0,255,0};
//蓝色
uint8_t BLUE[] = {0,0,255};
//青色    配网中
uint8_t CYAN[] = {0,255,255};
//紫色
uint8_t PURPLE[] = {255,0,255};

char _ID[] = "K09P761CJC";
char _NAME[] = "test01";
char _KEY[] = "R4pMO7+109oeaZJu3pM6Zw==";


int _TC,TCMQTTRCVPUB;
int key_status;
struct DATA all_data;

//懂得都懂
int run()
{
	printf( "Hello Master!\r\n");

	//赋初值
	_TC = TC_DISCONNECT;
	TCMQTTRCVPUB = FLAG_FLASE;
	key_status = KEY_NULL;
	buffer_flag = FLAG_FLASE;
	all_data.power_switch = RESET;

	//开启灯光
	WS_Load();
	WS281x_SetRGB(VIOLET);

	//接收电能数据
	HAL_UART_Receive_IT(&huart3, &electric_buffer,1);


	//开启串口接收
	HAL_UART_Receive_IT(&huart2, &buffer, 1);

	//提供软件运行时间
	HAL_TIM_Base_Start_IT(&htim16);

	//如果esp12已连接wifi，则直接连接腾讯云
	if(wait("IP",6000)!=AT_OK)
	{
		wifi_config();
	}

	 //配置 MQTT 连接参数
	 if(at_send("AT+TCMQTTCONN=1,5000,60,0,1\r\n",2000)==AT_OK)
	 {
		 printf("AT+TCMQTTCONN is ok\r\n");
	 }
	 //订阅
	 char data[256]={"\0"};
	 sprintf(data,"AT+TCMQTTSUB=\"$thing/down/property/%s/%s\",0\r\n",_ID,_NAME,all_data.power_switch);
	 if(at_send(data,2000)==AT_OK)
	 {
		 printf("AT+TCMQTTSUB is ok\r\n");
	 }
	 WS281x_SetRGB(GREEN);
	 _TC = TC_CONNECT;

	//关闭回显
	if(at_send("ATE0\r\n",2000)==AT_OK)
	{
		printf("ATE0 is ok\r\n");
	}

	//平台设备信息设置
	char data1[256] = {"\0"};
	sprintf(data1,"AT+TCDEVINFOSET=1,\"%s+\",\"%s\",\"%s\"\r\n",_ID,_NAME,_KEY);
	if(at_send(data1,2000)==AT_OK)
	{
		printf("AT+TCDEVINFOSET is ok\r\n");
		//释放变量空间
		free(data1);
	}

	while(1)
	{
		HAL_Delay(2000);

//
//		if(key_status == KEY_CENTER)
//		{
//			//配网
//			wifi_config();
//			key_status = KEY_NULL;
//			TCMQTTRCVPUB = FLAG_FLASE;
//			buffer_flag = FLAG_FLASE;
//			all_data.power_switch = RESET;
//			HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, RESET);
//			WS281x_SetRGB(GREEN);
//		}

		//防止串口卡死
		while(buffer_cnt != 0);

		if(_TC == TC_CONNECT)
		{
			int key_s;
			char data[256]={"\0"};

			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
					" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
					" \\\"power_switch\\\":%d } } \"\r\n",_ID,_NAME,all_data.power_switch);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);

			int tim=50;

			HAL_Delay(tim);


			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
						" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
						" \\\"current\\\":%0.1f } } \"\r\n",_ID,_NAME,all_data.current);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);

			HAL_Delay(tim);



			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
						" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
						" \\\"voltage\\\":%0.1f } } \"\r\n",_ID,_NAME,all_data.voltage);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);

			HAL_Delay(tim);


			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
						" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
						" \\\"power_factor\\\":%0.1f } } \"\r\n",_ID,_NAME,all_data.power_factor);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);

			HAL_Delay(tim);


			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
						" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
						" \\\"active_power\\\":%0.1f } } \"\r\n",_ID,_NAME,all_data.active_power);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);

			HAL_Delay(tim);


			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
						" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
						" \\\"apparent_power\\\":%0.1f } } \"\r\n",_ID,_NAME,all_data.apparent_power);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);

			HAL_Delay(tim);


			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
						" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
						" \\\"total_kwh\\\":%0.1f } } \"\r\n",_ID,_NAME,all_data.total_kwh);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);

			HAL_Delay(tim);


			sprintf(data,"AT+TCMQTTPUB=\"$thing/up/property/%s/%s\",0,\"{ \\\"method\\\":\\\"report\\\"\\,"
						" \\\"clientToken\\\":\\\"0\\\"\\,  \\\"timestamp\\\":0\\,\\\"params\\\":{  "
						" \\\"count_down\\\":%d } } \"\r\n",_ID,_NAME,all_data.count_down);
			HAL_UART_Transmit(&huart2, data, sizeof(data), 1000);
			wait("success",500);
			memset(data,0,256);



//			printf("sw:%d current:%0.1f  voltage:%0.1f  power_factor:%0.1f  active_power:%0.1f  apparent_power:%0.1f "
//					"total_kwh:%0.1f count_down:%d\r\n",all_data.power_switch,
//					all_data.current,all_data.voltage,all_data.power_factor,all_data.active_power,
//					all_data.apparent_power,all_data.total_kwh,all_data.count_down);
		}



	}
}






